package com.hong.pdf2img;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.*;
import java.util.Properties;

public class MainApplication extends Application {
    public static Properties properties;
    public static String inputPath="";
    public static String outputPath="";
    public static String img_format_value="png";    //图片格式
    public static Integer img_quality=100;      //DPI
    public static Integer img_type=1;  //图片类型 1-分页 2-单个长图
    public static Integer getImgType=1;  //生成方式 1-对每个页面截图 2-获取文档中的图片
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("main-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        stage.setTitle("PDF2Img");
        stage.setScene(scene);
        MainController mainController=fxmlLoader.getController();
        mainController.setOutPathInput(outputPath);
        mainController.setPrimaryStage(stage);
        mainController.setImgQualityInputFocusListener();   //执行添加焦点监听
        // 设置窗口关闭事件处理器
        stage.setOnCloseRequest(event -> {
            try {
                saveConf();  // 在窗口关闭时执行的操作
            } catch (IOException e) {}
        });
        stage.show();
    }

    public static void main(String[] args) throws IOException {
        loadConf();
        launch();
    }

    /** 载入配置 */
    private static void loadConf() throws IOException {
        File proFile=new File("application.properties");
        if(!proFile.exists()) proFile.createNewFile();
        BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(proFile),"utf-8"));
        if(properties==null) properties=new Properties();
//        System.out.println(properties);
        properties.load(reader);
        inputPath=properties.getProperty("input_path");
        outputPath=properties.getProperty("output_path");
        if(inputPath==null) inputPath="";
        if(outputPath==null) outputPath="";
        reader.close();
    }
    /** 保存配置 */
    private static void saveConf() throws IOException {
        File proFile=new File("application.properties");
        if(!proFile.exists()) proFile.createNewFile();
        OutputStreamWriter out=new OutputStreamWriter(new FileOutputStream(proFile,false),"utf-8");
        properties.setProperty("input_path",inputPath);
        properties.setProperty("output_path",outputPath);
        properties.store(out,"last_update");
        out.close();
    }

    // 错误对话框
    public static void showDialog(String title, String message,Alert.AlertType alertType) {
        Alert alert = new Alert(alertType);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }

    public static void showErrorDialog(String msg){
        showDialog("信息",msg,Alert.AlertType.ERROR);
    }

    public static void showInfoDialog(String msg){
        showDialog("信息",msg,Alert.AlertType.INFORMATION);
    }
    public static void showWarningDialog(String msg){
        showDialog("信息",msg,Alert.AlertType.WARNING);
    }
}
